﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
using System.Text;
using System.Data;

namespace ReportTracking
{
    public class DefinedSpeed
    {
        string FILE_NAME = "";
        public DefinedSpeed(string sPath)
        {
            this.FILE_NAME = sPath;
        }

        public void CreateFileXmlSpeed()
        {
            if (!File.Exists(FILE_NAME))
            {
                XmlDocument doc = new XmlDocument();
                XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
                doc.AppendChild(declaration);
                XmlElement xml = doc.CreateElement("SPEED");
                doc.AppendChild(xml);
                doc.Save(FILE_NAME);
                
            }

        }

        public bool InsertXML(string LoaiPT, string TenPT, float VanToc)
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(FILE_NAME);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();
                XmlNode currNode;
                XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
                docFrag.InnerXml = "<PT LPT=\"" + LoaiPT + "\">" +
                    "<TENPT>" + TenPT + "</TENPT>" +
                    "<VANTOC>" + VanToc + "</VANTOC>" +
                    
                    "</PT>";
                // insert the availability node into the document 
                currNode = doc.DocumentElement;
                currNode.InsertAfter(docFrag, currNode.LastChild);
                //save the output to a file 
                doc.Save(FILE_NAME);
                return true;
            }
            catch (System.Exception e)
            {
                return false;

            }

        }
        public bool DeleteRecord(string LPT)
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(FILE_NAME);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                //Select the cd node with the matching title
                XmlNode nodelist;
                XmlElement root = doc.DocumentElement;
                //pc = root.SelectSingleNode("//pc[@IPAddress='" + IP + "']");

                nodelist = root.SelectSingleNode("//PT[@LPT='" + LPT + "']");
                //root.RemoveChild(pc);

                if (nodelist != null)
                {
                    root.RemoveChild(nodelist);
                    
                }
                //save the output to a file 
                doc.Save(FILE_NAME);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

        public bool UpdateRecord(string LPT, string TENPT, float VanToc)
        {
            try
            {

                XmlTextReader reader = new XmlTextReader(FILE_NAME);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                //Select the cd node with the matching title
                XmlNode oldCd;
                XmlElement root = doc.DocumentElement;
                oldCd = root.SelectSingleNode("//PT[@LPT='" + LPT + "']");

                XmlElement newCd = doc.CreateElement("PT");
                newCd.SetAttribute("LPT", LPT);
                newCd.InnerXml = "<TENPT>" + TENPT + "</TENPT>" +
                                 "<VANTOC>" + VanToc + "</VANTOC>";
                root.ReplaceChild(newCd, oldCd);
                //save the output to a file 
                doc.Save(FILE_NAME);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

        public bool CheckExistLPT(string LPT)
        {
            try
            {

                XmlTextReader reader = new XmlTextReader(FILE_NAME);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                //Select the cd node with the matching title
                // XmlNode pc;
                XmlNode nodelist;
                XmlElement root = doc.DocumentElement;
                nodelist = root.SelectSingleNode("//PT[@LPT='" + LPT + "']");
                //pc = root.SelectSingleNode("//pc[@IPAddress='" + _IP + "']");

                if (nodelist == null)
                    return true;
                return false;

            }
            catch (System.Exception e)
            {
                return false;
            }
        }

        public DataSet LoadXML()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(FILE_NAME);
            return ds;
        }

    }
}